#include "AD7606.h"

void AD7606_SetParams(AD7606_InitHandle *InitHandle, AD7606_OS OsType, AD7606_RANGE RANGE)
{
	InitHandle->OsType = OsType;
	InitHandle->RANGE = RANGE;
}

void AD7606_SetOs(AD7606_InitHandle *InitHandle)
{
	uint8_t type = (uint8_t)InitHandle->OsType;

	InitHandle->Set_OS0((type&0x01));
	type = type>>1;
	InitHandle->Set_OS1((type&0x01));
	type = type>>1;
	InitHandle->Set_OS2((type&0x01));
	type = type>>1;
}

void AD7606_Set_Range(AD7606_InitHandle *InitHandle)
{
	InitHandle->Set_Range(InitHandle->RANGE);
}


void AD7606_Init(AD7606_InitHandle *InitHandle)
{
	AD7606_SetOs(InitHandle);
	AD7606_Set_Range(InitHandle);
}

void AD7606_Reset(AD7606_OptionHandle *OptionHandle)
{
	OptionHandle->AD7606_Reset(AD7606_Pin_Low);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_High);
	OptionHandle->AD7606_Reset(AD7606_Pin_Low);
}

void AD7606_Start(AD7606_OptionHandle *OptionHandle)
{
	OptionHandle->AD7606_Convst(AD7606_Pin_High);
	OptionHandle->AD7606_Convst(AD7606_Pin_Low);
	OptionHandle->AD7606_Delay(500/6+1);
	OptionHandle->AD7606_Convst(AD7606_Pin_High);
}


void AD7606_16bit_Merge(uint8_t buff_8bit[], uint16_t buff_16bit[], uint8_t AD7606_Size)
{
	int i;
	for(i = 0;i < 8; i++)
	{
		buff_16bit[i] = (buff_8bit[2*i]<<8) + buff_8bit[i*2+1];
	}
}

void AD7606_Voltage_Conversion(float *AD7606_Voltage, uint16_t *AD7606_16Bit_Buff, uint8_t AD7606_Size, AD7606_RANGE RANGE)
{
	int i,base_vol;
	base_vol = RANGE == AD7606_5V?5:10;
	for(i=0; i<AD7606_Size; i++)
	{
		AD7606_Voltage[i] = 1.0 * AD7606_16Bit_Buff[i] * base_vol / 32767;
	}
}

void AD7606_Callback_8bit(AD7606_OptionHandle *OptionHandle, uint8_t *pData, uint8_t Size)
{
		OptionHandle->AD7606_Recive(pData,Size);
		AD7606_16bit_Merge((uint8_t *)OptionHandle->AD7606_8Bit_Buff, (uint16_t *)OptionHandle->AD7606_16Bit_Buff, OptionHandle->AD7606_Size);
		AD7606_Start(OptionHandle);
}






